package service

import(
	"net/http"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"github.com/unrolled/render"
)

func NewServer() *negroni.Negroni {
	formatter := render.New(render.Options{IndentJSON:true})

	n := negroni.Classic();
	mx := mux.NewRouter();

	initRoutes(mx,formatter)
	n.UseHandler(mx)

	return n
}

func initRoutes(m *mux.Router, formatter *render.Render){
	m.HandleFunc("/test",testHandFunc(formatter)).Methods("GET")
	m.HandleFunc("/holle",holleHandFunc(formatter)).Methods("GET")
}

func testHandFunc(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request){
		formatter.JSON(w,http.StatusOK,struct{Test string}{"This a test"})
	}
}

func holleHandFunc(formatter *render.Render) http.HandlerFunc{
	return func(w http.ResponseWriter, req *http.Request){
		formatter.JSON(w,http.StatusOK,struct{Holle string}{"Go的世界！"})
	}
}